package com.baomidou.springwind.controller;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.framework.controller.SuperController;
import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.common.HoorayosHelper;
import com.baomidou.springwind.entity.Member;
import com.baomidou.springwind.entity.Setting;
import com.baomidou.springwind.entity.Wallpaper;
import com.baomidou.springwind.service.IMemberService;
import com.baomidou.springwind.service.ISettingService;
import com.sun.tools.internal.ws.wsdl.framework.Entity;

/**
 * <p>
 *  皮肤控制器
 * </p>
 *
 * @author Koumiba
 * @since 2017-03-24
 */
@Controller
@RequestMapping("/sysapp/skin")
public class SkinController extends SuperController {
	@Autowired
	private IMemberService memberService;
	@Autowired
	private ISettingService settingService;
	/**
	 * 主页
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/index")
	public String index(Model model) {
		
		model.addAttribute("errorCode", HoorayosHelper.getErrorCodeMap());
		//验证是否登入
		if(!HoorayosHelper.checkLogin(request)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noLogin"));
			return "error";
		}
		
		//读取皮肤目录
		String skinBody = "";
		String rootPath = session.getServletContext().getRealPath("/")+"/WEB-INF/static/img/skins";
		File file = new File(rootPath);
		String[] tempList = file.list();
		ArrayList<Map> fileArr = new ArrayList<Map>();
		for(int i = 0; i<tempList.length; i++){
			File temp = new File(rootPath+File.separator+tempList[i]);
			if(HoorayosHelper.isEndWithCss(tempList[i])){
				Map<String,String> m = new HashMap<String,String>();
				m.put("name",  HoorayosHelper.getFileBaseName(tempList[i]));
				m.put("img", "img/skins/"+ m.get("name")+"/preview.png");
				fileArr.add(m);
			}			
		}
		if(fileArr.size() > 0){
			String skin = getSkin();
			for(Map<String,String> f : fileArr){
				if(f.get("name").equals(skin)){
					skinBody += "<li class='selected' skin='"+f.get("name")+"'><img src='/static/"+f.get("img")+"' style='width:256px;height:156px'><div></div></li>";
				}else{
					skinBody += "<li skin='"+f.get("name")+"'><img src='/static/"+f.get("img")+"' style='width:256px;height:156px'><div></div></li>";
				}
				
			}			
		}				
				
		model.addAttribute("skinBody", skinBody);	
		return "/sysapp/skin/index";
	}
	
	/**
	 * ajax页面
	 */
	@ResponseBody
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/ajax")
	public void ajax(String ac) {
		if (ac.equals("update")){
			Member member = memberService.selectById(HoorayosHelper.getMemberId(request));
			member.setSkin(request.getParameter("skin"));
			memberService.insertOrUpdate(member);
		}		
	}
	
	/**
	 * 获取皮肤
	 * @return String
	 * koumiba
	 * 2017年4月4日
	 */
	private String getSkin(){
		String skin = "";
		int mid = HoorayosHelper.getMemberId(request);
		if(mid > 0){
			Member member = memberService.selectById(mid);
			skin = member.getSkin();
		}else{
			Setting set = settingService.selectById(1);
			skin = set.getSkin();
		}
		
		return skin;
	}
}
